package weaver.interfaces.qz.job;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.kq.service.KQReportService;
import com.engine.kq.service.impl.KQReportServiceImpl;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.hrm.User;
import weaver.interfaces.qz.job.util.AttendanceDataExtractor;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2025/11/3
 * @Description 考勤相关定时任务
 * @Author Lee
 */
public class kqCustomCornJob extends BaseCronJob {

    private KQReportService getService(User user) {
        return ServiceUtil.getService(KQReportServiceImpl.class, user);
    }

    BaseBean bb = new BaseBean();
    private String fromDate;
    private String toDate;
    private String month;


    @Override
    public void execute() {
        RecordSet rs = new RecordSet();
        //只是作为线程在执行的标识
        String flagSql = "select id from employee_attendance where ssyf=?";
        rs.executeQuery(flagSql, "threadRunning");
        if (rs.next()) {
            return;
        }
        rs.executeUpdate("insert into employee_attendance (ssyf) values('threadRunning')");

        if (StringUtils.isEmpty(fromDate)) {
            fromDate = TimeUtil.getFirstDayOfMonth();
            toDate = TimeUtil.getMonthEndDay();
        }
        if (StringUtils.isNotEmpty(month)) {
            fromDate = month + "-01";
            toDate = TimeUtil.getMonthEndDay(fromDate);
        }
        bb.writeLog("kqCustomCornJob start.");
        User user = new User();
        user.setUid(1);
        user.setLanguage(7);

        HashMap<String, Object> map = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("pageIndex", 1);
        params.put("typeselect", "6");
        params.put("fromDate", fromDate);
        params.put("toDate", toDate);
        params.put("viewScope", "0");
        params.put("status", "8");
        params.put("isNoAccount", "1");
        params.put("attendanceSerial", "");
        String paramJson = JSONObject.toJSONString(params);
        map.put("data", paramJson);

        Map<String, Object> kqReport = getService(user).getKQReport(map, user);
        HashSet<String> monthSet = new HashSet<>();
        // 先操作第一页，提取数据
        AttendanceDataExtractor dataExtractor = new AttendanceDataExtractor();
        List<List> attendanceList = dataExtractor.extractAttendanceData(kqReport, monthSet);
        //剩余页数数据处理
        int pagecount = (int) kqReport.get("pagecount");
        for (int i = 2; i <= pagecount; i++) {
            params.put("pageIndex", i);
            map.put("data", JSONObject.toJSONString(params));
            kqReport = getService(user).getKQReport(map, user);
            List<List> lists = dataExtractor.extractAttendanceData(kqReport, monthSet);
            attendanceList.addAll(lists);
        }
        bb.writeLog("attendanceList-" + attendanceList);
        if (!attendanceList.isEmpty()) {
            //先去删除所属月数据
            String months = monthSet.stream().map(s -> "'" + s + "'").collect(Collectors.joining(","));
            String deleteSql = "delete from employee_attendance where ssyf in (" + months + ")";
            rs.execute(deleteSql);
            String sql = "INSERT INTO employee_attendance (\n" + "            lastname, workcode, ssyf, workdays, workmins, attendancedays, \n" + "            attendanceMins, signmins, sjycqts, sjycqss, ypjcqsc, sjdksc, \n" + "            ypjdksc, dygzrts, dyfdjrts, dyzhyggzr, qyjjrts, kgtsqz, \n" + "            leaveType_3, overtime_4leave, overtime_nonleave, businessLeave, officialBusiness\n" + "        ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            rs.executeBatchSql(sql, attendanceList);
        }
        rs.executeUpdate("delete from  employee_attendance where ssyf ='threadRunning'");

    }


    public String getFromDate() {
        return fromDate;
    }

    public void setFromDate(String fromDate) {
        this.fromDate = fromDate;
    }

    public String getToDate() {
        return toDate;
    }

    public void setToDate(String toDate) {
        this.toDate = toDate;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public String getMonth() {
        return month;
    }
}
